﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum PlayerState
{
    Span,
    Idle,
    Run,
    Climb,
    Attack,
    Attacked,
    Die
}

public class PlayerController : MonoBehaviour
{
    [Header("基础属性")]
    public float speed = 10;
    //public float movement = 10;
    public float maxHP = 100;

    [Header("额外状态")]
    public bool HaveKinfe = false;
    public bool HaveHammer = false;


    //[Header("基础组件（无需赋值）")]
    [HideInInspector]public Animator anim;
    [HideInInspector] public Rigidbody2D rb;
    [HideInInspector] public SpriteRenderer srer;
    [HideInInspector] public Transform tf;
    public PlayerState ps = PlayerState.Idle;
    //状态机
    public StateMachine machine;
    void Start()
    {
        anim = GetComponent<Animator>();
        rb= GetComponent<Rigidbody2D>();
        srer = GetComponent<SpriteRenderer>();
        //初始化状态类
        IdleState idle = new IdleState(1, this);
        RunState run = new RunState(2, this);
        AtkState atk = new AtkState(3, this);
        //状态机
        machine = new StateMachine(idle);
        machine.AddState(run);
        machine.AddState(atk);

    }

    // Update is called once per frame
    void Update()
    {
        if ((Input.GetKey(KeyCode.A)|| Input.GetKey(KeyCode.D))&&ps!=PlayerState.Attack)
        {
            if((Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))){

            }
            else
            {
                if (Input.GetKey(KeyCode.A))
                {
                    //srer.flipX = false;
                    transform.localScale = new Vector3 (1,1,1);
                }
                if (Input.GetKey(KeyCode.D))
                {
                    transform.localScale = new Vector3(-1, 1, 1);
                }
                ps = PlayerState.Run;
            }
        }
        else
        {
            if(ps== PlayerState.Run)
            ps = PlayerState.Idle;
        }
        if (Input.GetKeyDown(KeyCode.J)&& ps!= PlayerState.Attack)
        {
            ps = PlayerState.Attack;
        }
        if (Input.GetKeyDown(KeyCode.K))
        {
            anim.SetTrigger("isIdleThing");
        }



        //if (!Event.current.isKey)
        //{
        //    ps = PlayerState.Idle;
        //}

        machine.Update();
        UpdateAnimation();
       
    }
    private void FixedUpdate()
    {
        machine.FixedUpdate();

    }
    private void UpdateAnimation()
    {
        switch (ps)
        {
            case PlayerState.Idle:
                machine.TranslateState(1);
                break;
            case PlayerState.Run:
                machine.TranslateState(2);
                break;
            case PlayerState.Attack:
                machine.TranslateState(3);
                break;
        }
    }
    void LateUpdate()
    {
        
    }
    public void fix_idle()
    {
        ps = PlayerState.Idle;
    }

    public void EndAtk2()
    {
        ps = PlayerState.Idle;
    }
}
